package menu 
{
	import flash.utils.Dictionary;
	import utils.T;
	import vo.CastleVO;
	import vo.GeneralVO;
	import vo.PlayerVO;
	/**
	 * ...
	 * @author WLDragon 2014-03-29
	 */
	public class Verify 
	{
		
		public function Verify() 
		{
			
		}
		
		static private var _instance:Verify;
		static public function get instance():Verify
		{
			return _instance ||= new Verify();
		}
		
		/**
		 * 验证参数是否合法
		 * @param	verifyCode 自定义验证代码
		 * 1 验证城池是否为己方城池或空城并可达
		 * 2 验证金是否足够
		 * 3 验证是否需要读书
		 * 4 验证城池是否为敌方城池
		 * 5 验证是否不是君主
		 * 6 验证城池是否为己方并可达
		 * 7 验证城池可达而且是敌方或空白
		 * @param	param
		 * @return
		 */
		public function test(verifyCode:int,param:*):Boolean
		{
			if (verifyCode == 1)
			{
				//检测是否与当前城池连通，是否为自己的城池或空城
				if (targetCastles.indexOf(String(param)) == -1)
				{
					Msg.instance.show("无法移动至此\n请指定其他城池",160,332);
					return false;
				}
			}
			else if (verifyCode == 2)
			{
				//检测是否有足够的金
				if (Gbl.activeCastle.gold < param)
					return false;
			}
			else if (verifyCode == 3)
			{
				var g:GeneralVO = T.db.getRecord(Cnt.VO_GENERAL, param);
				if (g.knowledge >= 80)
					return false;
			}
			else if (verifyCode == 4)
			{
				var c:CastleVO = T.db.getRecord(Cnt.VO_CASTLE, param);
				if (c.playerID == 0)
				{
					Msg.instance.show("此城并无武将",160,332);
					return false;
				}
				else if (Gbl.activePlayer.castles.indexOf(String(param)) > -1)
				{
					Msg.instance.show("此乃己方城池", 160, 332);
					return false;
				}
			}
			else if (verifyCode == 5)
			{
				var ps:Dictionary = T.db.getTable(Cnt.VO_PLAYER);
				for each(var p:PlayerVO in ps)
				{
					if (p.monarchID == param)
					{
						Msg.instance.show("此武将乃君主也", 80, 415);
						return false;
					}
				}
			}
			else if (verifyCode == 7)
			{
				if (targetCastles.indexOf(String(param)) == -1)
				{
					Msg.instance.show("无法出征至该城");
					return false;
				}
			}
			
			return true;
		}
		
		public function getCastles(verifyCode:int,target:CastleVO):Array
		{
			var t:Array = [];
			var c:CastleVO;
			var x:XML;
			var a:Array;
			var id:String;
			if (verifyCode == 1)
			{
				//获取连通并（己方或空白）
				x = T.db.getXml(Cnt.XML_CASTLE).castle.(@id == String(target.id))[0];
				a = String(x.@conList).split(',');//与当前城池连通的城池
				for each(id in a)
				{
					c = T.db.getRecord(Cnt.VO_CASTLE, int(id));
					if (c.playerID == 0 || c.playerID == Gbl.activePlayer.id)
						t.push(id);
				}
				targetCastles = t;
			}
			else if (verifyCode == 4)
			{
				//所有敌方城池
				var cs:Dictionary = T.db.getTable(Cnt.VO_CASTLE);
				for each(c in cs)
				{
					if (c.playerID != 0 && c.playerID != Gbl.activePlayer.id)
						t.push(c.id);
				}
			}
			else if (verifyCode == 6)
			{
				//获取己方并连通
				x = T.db.getXml(Cnt.XML_CASTLE).castle.(@id == String(target.id))[0];
				a = String(x.@conList).split(',');//与当前城池连通的城池
				for each(id in a)
				{
					if (Gbl.activePlayer.castles.indexOf(id) > -1)
						t.push(id);
				}
			}
			else if (verifyCode == 7)
			{
				//获取连通并（敌方或空白）
				x = T.db.getXml(Cnt.XML_CASTLE).castle.(@id == String(target.id))[0];
				a = String(x.@conList).split(',');//与当前城池连通的城池
				for each(id in a)
				{
					c = T.db.getRecord(Cnt.VO_CASTLE, int(id));
					if (c.playerID == 0 || c.playerID != Gbl.activePlayer.id)
						t.push(id);
				}
				targetCastles = t;
			}
			
			return t;
		}
		
		/**
		 * 验证城池的连通性,暂时作废****
		 * @param	targetId
		 */
		private function verifyConnection(target:CastleVO):Boolean
		{
			var x:XML = T.db.getXml(Cnt.XML_CASTLE).castle.(@id == target.id.toString())[0];
			var a:Array = String(x.@conList).split(',');
			var isCon:Boolean = a.indexOf(Gbl.activeCastle.id.toString()) > -1;
			return isCon;
		}
		
		/**缓存getCastles获得的城池，在验证参数时用*/
		private var targetCastles:Array;
	}

}